package com.whiteape.book.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.whiteape.book.R;
import com.whiteape.book.core.manager.ReadSettingManager;
import com.whiteape.book.core.page.PageLoader;
import com.whiteape.book.core.page.PageMode;
import com.whiteape.book.core.page.PageStyle;
import com.whiteape.book.core.page.PageTextIntervalMode;
import com.whiteape.book.ui.activity.BookReadActivity;
import com.whiteape.book.ui.activity.MoreSettingActivity;
import com.whiteape.book.ui.activity.TypefaceManagerActivity;
import com.whiteape.book.ui.adapter.PageStyleAdapter;
import com.whiteape.book.ui.fragment.ReadSettingAdvancedFragment;
import com.whiteape.book.ui.fragment.ReadSettingCommonFragment;
import com.whiteape.book.ui.fragment.ReadSettingPageStyleFragment;
import com.whiteape.book.utils.BrightnessUtils;
import com.whiteape.book.utils.DisplayUtil;
import com.whiteape.book.utils.ScreenUtils;
import com.whiteape.book.utils.SystemBarUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.ButterKnife;

/**
 */

public class BookReadSettingDialogFragment extends DialogFragment {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mTitleList = new ArrayList<>();
    private PageLoader mPageLoader;

    public BookReadSettingDialogFragment(/*Activity activity, PageLoader pageLoader*/) {
    }

    public ReadSettingCommonFragment getReadSettingCommonFragment() {
        if(mFragmentList == null || mFragmentList.size() == 0) {
            return null;
        }
        return (ReadSettingCommonFragment) mFragmentList.get(0);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        WindowManager.LayoutParams params = getDialog().getWindow()
                .getAttributes();
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        params.windowAnimations = R.style.anim_slide_bottom_in_out;
        getDialog().getWindow().setAttributes(params);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

//        DisplayMetrics dm = new DisplayMetrics();
//        getDialog().getWindow().setLayout(dm.widthPixels, ViewGroup.LayoutParams.WRAP_CONTENT);


        if (mFragmentList.size() == 0) {
            mFragmentList.add(new ReadSettingCommonFragment());
            mFragmentList.add(new ReadSettingPageStyleFragment());
            mFragmentList.add(new ReadSettingAdvancedFragment());
        }
        if (mTitleList.size() == 0) {
            mTitleList.add("常用");
            mTitleList.add("排版");
            mTitleList.add("其他");
        }

        View root = inflater.inflate(R.layout.dialog_read_setting, container, false);
        mViewPager = root.findViewById(R.id.viewpager);
        mViewPager.setOffscreenPageLimit(mFragmentList.size());
        mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

        mTabLayout = root.findViewById(R.id.tab_tl_indicator);
        mTabLayout.setupWithViewPager(mViewPager);

        mViewPager.setCurrentItem(0);

        setUpWindow();
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getDialog() == null || getDialog().getWindow() == null || getContext() == null) {
            return;
        }
        getDialog().getWindow().setLayout(DisplayUtil.getScreenWidth(getContext()), DisplayUtil.dip2px(getContext(),360));

    }

    //设置Dialog显示的位置
    private void setUpWindow() {
        if(getDialog() == null) {
            return;
        }
        Window window = getDialog().getWindow();
        if(window == null){
            return;
        }
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
    }
    private class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public int getItemPosition(@NonNull Object object) {
            return POSITION_NONE;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);
        }
    }
}

